<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>let经典案例</title>
    <style>
        .container>.item {
            width: 150px;
            height: 80px;
            display: inline-block;
            border: 1px solid red;
            margin-right: 10px;
        }
    </style>
</head>

<body>
    <div class="container">
        <h2 class="page-header">点击切换颜色</h2>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
    </div>

    <script>
        let items = document.getElementsByClassName('item');

        // [...items].forEach(item => {
        //     item.onclick = function () {
        //         console.log(this)
        //         this.style.background = 'pink'
        //     }
        // });

        // for (var i = 0; i < items.length; i++) {
        for (let i = 0; i < items.length; i++) {
            items[i].onclick = function() {
                //this.style.background = 'pink'
                
                console.log(i)
                items[i].style.background = 'pink'
            }
        }
    </script>
</body>

</html>